package com.andy.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

public class CoinInfoDao {
    private String TAG = "CoinInfoDao";
    private DBManager mDBManager;

    public CoinInfoDao(Context context)
    {
        mDBManager = DBManager.getInstance(context);
    }

    /**
     * 插入
     *
     * @param item
     */
    public long insert(CoinInfo item) throws Exception
    {
        ContentValues values = new ContentValues();
        values.put(CoinInfo.Position, item.getPosition());
        values.put(CoinInfo.Coin_name, item.getCoin_name());
        values.put(CoinInfo.Addr, item.getAddr());
        values.put(CoinInfo.Price, item.getPrice());
        values.put(CoinInfo.Market_value, item.getMarket_value());
        values.put(CoinInfo.People_number, item.getPeople_number());
        values.put(CoinInfo.Trade_volume, item.getTrade_volume());
        values.put(CoinInfo.Trade_number, item.getTrade_number());
        values.put(CoinInfo.Save_time, item.getSave_time());
        return mDBManager.insert(CoinInfo.TABLE_NAME, values);
    }


    /**
     * 更新
     *
     * @param
     */
    public void update(CoinInfo circle)
    {

        String WhereClause = CoinInfo.ID + " = ?  ";

        String[] whereArgs = new String[]{
                String.valueOf(circle.getId()),
        };

        ContentValues values = new ContentValues();
        values.put(CoinInfo.ID, circle.getId());
        values.put(CoinInfo.Position, circle.getPosition());
        values.put(CoinInfo.Coin_name, circle.getCoin_name());
        values.put(CoinInfo.Addr, circle.getAddr());
        values.put(CoinInfo.Price, circle.getPrice());
        values.put(CoinInfo.Market_value, circle.getMarket_value());
        values.put(CoinInfo.People_number, circle.getPeople_number());
        values.put(CoinInfo.Trade_volume, circle.getTrade_volume());
        values.put(CoinInfo.Trade_number, circle.getTrade_number());
        values.put(CoinInfo.Save_time, circle.getSave_time());

        int count = mDBManager.update(CoinInfo.TABLE_NAME, values, WhereClause, whereArgs);
    }

    /**
     * 查询全部历史列表
     *
     * @param
     */
    public List<CoinInfo> queryAll() {
        String sql = "select * from " + CoinInfo.TABLE_NAME;
        String[] args = new String[] {
        };

        List<CoinInfo> list = new ArrayList<CoinInfo>();
        try {
            Cursor cursor = mDBManager.rawQuery(sql, args);
            try {
                if (cursor != null) {
                    while (cursor.moveToNext()) {
//                        Log.e(TAG, "queryAll_sql="+sql);
                        list.add(pack(cursor));
                    }

                }
            } finally {
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;

    }

    public List<CoinInfo> queryGroupByAddr(){
        String sql = "select * from " + CoinInfo.TABLE_NAME + " GROUP BY  " + CoinInfo.Addr + " ";
        String[] args = new String[]{
        };
        List<CoinInfo> list = new ArrayList<CoinInfo>();
        try
        {
            Cursor cursor = mDBManager.rawQuery(sql, args);

            try
            {
                if (cursor != null)
                {
                    while (cursor.moveToNext()) {
//                        Log.e(TAG, "queryAll_sql="+sql);
                        list.add(pack(cursor));
                    }
                }
            }
            finally
            {
                cursor.close();
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return list;
    }
    public List<CoinInfo> queryByAddr(String addr){
        String sql = "select * from " + CoinInfo.TABLE_NAME + " where " + CoinInfo.Addr + " = ? ";
        String[] args = new String[]{
                addr
        };
        List<CoinInfo> list = new ArrayList<CoinInfo>();
        try
        {
            Cursor cursor = mDBManager.rawQuery(sql, args);

            try
            {
                if (cursor != null)
                {
                    while (cursor.moveToNext()) {
                        list.add(pack(cursor));
                    }
                }
            }
            finally
            {
                cursor.close();
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据id查询
     *
     * @param
     */
    public CoinInfo queryById(int id)
    {

        String sql = "select * from " + CoinInfo.TABLE_NAME + " where " + CoinInfo.ID + " = ? ";
        String[] args = new String[]{
                String.valueOf(id),

        };
        try
        {
            Cursor cursor = mDBManager.rawQuery(sql, args);

            try
            {
                if (cursor != null)
                {
                    while (cursor.moveToNext())
                    {
                        return pack(cursor);
                    }
                }
            }
            finally
            {
                cursor.close();
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 根据路径删除某条记录
     * 根据时间来删除的
     *
     * @throws
     */
    public void deleteById(String id) throws Exception
    {
        String WhereClause = CoinInfo.ID + " = ?";
        String[] whereArgs = new String[]{String.valueOf(id),};
        mDBManager.delete(CoinInfo.TABLE_NAME, WhereClause, whereArgs);
    }


    @SuppressLint("Range")
    public CoinInfo pack(Cursor cursor)
    {
        CoinInfo circle = new CoinInfo();
        circle.setId(cursor.getInt(cursor.getColumnIndex(CoinInfo.ID)));
        circle.setPosition(cursor.getInt(cursor.getColumnIndex(CoinInfo.Position)));
        circle.setCoin_name(cursor.getString(cursor.getColumnIndex(CoinInfo.Coin_name)));
        circle.setAddr(cursor.getString(cursor.getColumnIndex(CoinInfo.Addr)));
        circle.setPrice(cursor.getString(cursor.getColumnIndex(CoinInfo.Price)));
        circle.setMarket_value(cursor.getString(cursor.getColumnIndex(CoinInfo.Market_value)));
        circle.setPeople_number(cursor.getString(cursor.getColumnIndex(CoinInfo.People_number)));
        circle.setTrade_volume(cursor.getString(cursor.getColumnIndex(CoinInfo.Trade_volume)));
        circle.setTrade_number(cursor.getString(cursor.getColumnIndex(CoinInfo.Trade_number)));
        circle.setSave_time(cursor.getString(cursor.getColumnIndex(CoinInfo.Save_time)));

        return circle;
    }
}
